
package com.cl.mvc.dto;

import com.cl.mvc.util.ResultCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
public class JDCommonResult<T> {
    private long code;
    private String msg;
    private DataObject<T> data;

    protected JDCommonResult() {
    }

    private JDCommonResult(long code, String msg, DataObject<T> data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> JDCommonResult<T> success(List<T> data) {
        return new JDCommonResult<T>(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), new DataObject<>(data));
    }
    public static <T> JDCommonResult<T> success(List<T> data,Integer count) {
        return new JDCommonResult<T>(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), new DataObject<>(data,count));
    }
    public static <T> JDCommonResult<T> success(List<T> data,Integer count,String msg) {
        return new JDCommonResult<T>(ResultCodeEnum.SUCCESS.getCode(),msg, new DataObject<>(data,count));
    }

    public static <T> JDCommonResult<T> success(List<T> data, String msg) {
        return new JDCommonResult<T>(ResultCodeEnum.SUCCESS.getCode(), msg, new DataObject<>(data));
    }

    public static <T> JDCommonResult<T> failed(ResultCodeEnum resultCodeEnum) {
        return new JDCommonResult<T>(resultCodeEnum.getCode(), resultCodeEnum.getMessage(), null);
    }

    public static <T> JDCommonResult<T> failed(String message) {
        return new JDCommonResult<T>(ResultCodeEnum.FAILED.getCode(), message, null);
    }

    public static <T> JDCommonResult<T> failed() {
        return failed(ResultCodeEnum.FAILED);
    }

    public static <T> JDCommonResult<T> validateFailed() {
        return failed(ResultCodeEnum.VALIDATE_FAILED);
    }

    public static <T> JDCommonResult<T> validateFailed(String message) {
        return new JDCommonResult<T>(ResultCodeEnum.VALIDATE_FAILED.getCode(), message, null);
    }

    public static <T> JDCommonResult<T> unauthorized() {
        return new JDCommonResult<T>(ResultCodeEnum.UNAUTHORIZED.getCode(), ResultCodeEnum.UNAUTHORIZED.getMessage(), null);
    }

    public static <T> JDCommonResult<T> forbidden() {
        return new JDCommonResult<T>(ResultCodeEnum.FORBIDDEN.getCode(), ResultCodeEnum.FORBIDDEN.getMessage(), null);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    private static class DataObject<T> {
        private List<T> list;
        private Integer total;

        public DataObject(List<T> list) {
            this.list = list;
        }
    }
}
